package top.waterduck.server.service;

import top.waterduck.server.pojo.Test;
import top.waterduck.server.dto.TestDto;
import top.waterduck.server.pojo.TestExample;
import top.waterduck.server.dto.PageDto;
import top.waterduck.server.mapper.TestMapper;
import top.waterduck.server.utils.CopyUtil;
import top.waterduck.server.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;
/**
* @author: duck
* @date: 2020-09-17 19:41
* @desc: 服务类
**/
@Service
public class TestService {
    @Resource
    private TestMapper testMapper;

    /**
    * 获取列表
    * @param pageDto
    */
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        TestExample testExample=new TestExample();
        List<Test> testList = testMapper.selectByExample(testExample);
        PageInfo<Test>pageInfo=new PageInfo<>(testList);
        pageDto.setTotal(pageInfo.getTotal());
        List<TestDto> testDtoList = CopyUtil.copyList(testList,TestDto.class);
        pageDto.setList(testDtoList);
    }

    /**
    * 保存信息
    * @param testDto
    */
    public void save(TestDto testDto){
        Test test= CopyUtil.copy(testDto,Test.class);
        if(StringUtils.isEmpty(testDto.getId())){
        this.insert(test);
        }else {
        this.update(test);
        }
    }

    /**
    * 新增
    * @param test
    */
    private void insert(Test test){
        testMapper.insert(test);
    }

    /**
    * 更新
    * @param test
    */
    private void update(Test test){
        testMapper.updateByPrimaryKey(test);
    }

    /**
    * 删除信息
    * @param id
    */
     public void delete(String id){
        testMapper.deleteByPrimaryKey(id);
     }



}
